from DrissionPage import ChromiumPage, ChromiumOptions
import time
from openpyxl import Workbook
import datetime
import logging as log

log.basicConfig(level=log.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

co1 = ChromiumOptions().set_local_port(9222).set_user_data_path(r'D:\data\drissionpage\9222')
page = ChromiumPage(co1)


class Contribute:
    def __init__(self,page,start_url=""):
        self.page = page
        self.start_url = start_url
        page.listen.start('/space/wbi/arc/search')
        page.get(start_url)

    def page_videos(self):
        """
        提取出所有的视频信息
        :return:
        """
        packet = page.listen.wait()  # 等待数据包\
        return packet.response.body['data']['list']['vlist']

    def video_info(self,item):
        """
        提取出视频信息
        :param item:
        :return:
        """
        info = {}
        info['title'] = item['title']
        info['bv'] = item['bvid']
        info['description'] = item['description']
        info['length'] = item['length']
        info['created'] = item['created']
        info['created_str'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(item['created']))
        info['play'] = item['play']
        info['comment'] = item['comment']
        return info


    def has_next(self):
        """
        是否有下一页
        :return:
        """
        if self.page.ele('x://li[@title="下一页"][not(contains(@class,"disabled"))]'):
            return True
        return False

    def click_next(self):
        """
        点击下一页
        :return:
        """
        self.page.ele('x://li[@title="下一页"][not(contains(@class,"disabled"))]').click()



def save_to_excel(data_list,file_name):
    """
    将数据保存到excel
    :param data_list:
    :param file_name:
    :return:
    """
    wb = Workbook()
    ws = wb.active
    ws.append(["标题","BV","描述","长度","投稿时间戳","投稿时间","播放数","评论数"])
    for data in data_list:
        ws.append(data)
    wb.save(file_name)


def main():
    page = ChromiumPage()
    start_url = "https://space.bilibili.com/384876532/video"
    contribute = Contribute(page, start_url)
    first_page_vidios = contribute.page_videos()
    data_list = []
    for video in first_page_vidios:
        video_info = contribute.video_info(video)
        data_list.append(list(video_info.values()))
    while contribute.has_next():
        contribute.click_next()
        next_page_vidios = contribute.page_videos()
        for video in next_page_vidios:
            video_info = contribute.video_info(video)
            data_list.append(list(video_info.values()))
        time.sleep(10)
    # save
    now = datetime.datetime.now()
    file_name = "D:\\bilibili_%s.xlsx" % now.strftime("%Y%m%d")
    save_to_excel(data_list, file_name)


if __name__=="__main__":
    main()